<template>
  <div  :class="className" :style="{marginLeft:marginleft,height:height,width:width}"/>
</template>

<!--行情通用曲线-->
<script>
import * as echarts from 'echarts'
const unwarp = (obj) => obj && (obj.__v_raw || obj.valueOf() || obj);
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '90%'
    },
    height: {
      type: String,
      default: '600px'
    },
    marginleft: {
      type: String,
      default: '5%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  // mounted() {
  //   console.log('mounted')
  //   this.$nextTick(() => {
  //     this.initChart()
  //   })
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);
    },
    clearCharData() {
      this.chart.dispose();
      this.chart = null
      // this.chart = echarts.init(this.$el, 'macarons');
      // this.setOptions({'titleText':'','legend':[],'xAxisData':[],'seriesName':[],'seriesData':[]});
    },
    /**
     * 曲线调用数据处理方法
     * @param listData 曲线数据
     * @param xName   横坐标字段名
     * @param yNames   纵坐标字段名的列表
     * @param curveName 曲线名称list
     * @param seriesNames 曲线list
     */
    setChartData(proxy,title,listData,xName,yNames,curveNames,seriesNames,xAxisLabel,yAxisLabel,xAxisData){
      let seriesData={};
      let legend=[];
      //初始化seriesData的值为数组
      curveNames.forEach(name => {
        seriesData[name] = [];
      })
      curveNames.forEach(curveName =>{
        legend.push(curveName);
      })
      for (let i = 0; i < listData.length; i++) {
        let indexPrice =  listData[i];
        yNames.forEach(yName => {
          seriesData[indexPrice['address']].push(indexPrice[yName]);
        })
      }
      this.chartData.titleText = title;
      this.chartData.legend = legend;
      this.chartData.xAxisData = xAxisData;
      this.chartData.seriesName = seriesNames;
      this.chartData.seriesData= seriesData;
      this.chartData.xAxisLabel = xAxisLabel;
      this.chartData.yAxisLabel = yAxisLabel;
      this.initChart(this.chartData)
    },
    setOptions(expectedData) {
      unwarp(this.chart).setOption({
        title: {
          text: expectedData.titleText,
          left:"center",
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {
          data: expectedData.legend,
          top:'6%'
        },
        grid: {
          left: '6%',
          right: '3%',
          bottom: '10%',
          top: '20%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          },
        },
        xAxis: {
          axisLine: {
            symbol: ['none', 'none']
          },
          axisTick: {
            show: true,
            alignWithLabel: true,
            length: 2,
            inside: true,
          },
          name: expectedData.xAxisLabel,
          nameTextStyle:{
            fontSize: 16
          },
          nameLocation:'middle',
          nameGap:30,
          type: 'category',
          boundaryGap: ['5%', '5%'],
          data: expectedData.xAxisData
        },
        yAxis: {
          show:true,
          axisLine: {
            symbol: ['none', 'arrow']
          },
          name: expectedData.yAxisLabel,
          nameTextStyle:{
            fontSize: 16
          },
          min: 'dataMin',
          type: 'value'
        },
        series: this.setSeries(expectedData)
      },false);
    },
    setSeries(chartData){
      let series=[];
      if(chartData.seriesName!=null&&chartData.seriesName.length>0){
            for(let i = 0; i < chartData.seriesName.length; i++) {
              let item = {};
              item.name = chartData.legend[i];
              item.type = 'line';
              item.data = chartData.seriesData[chartData.seriesName[i]];
              item.smooth = true;
              series.push(item);
            }
          return series;
      }else{
        return [];
      }
    },
  }
}
</script>
